package edu.hubu.talentmanagementsystem.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Enumeration;

/**
 * @author moonlan
 * date 2021/3/10 下午12:42
 */
@SuppressWarnings(value = "unused")
public class UrlInterceptor implements HandlerInterceptor {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.debug("--------------------------------------------");
        String host = request.getRemoteHost();
        logger.debug("访问者 IP --> {}", host);
        String uri = request.getRequestURI();
        logger.debug("访问 URI --> {}", uri);
        Cookie[] cookies = request.getCookies();
        logger.debug("携带的 COOKIES --> {}", Arrays.toString(cookies));
        String queryString = request.getQueryString();
        logger.debug("URL参数 QUERY_STRING --> {}", queryString);
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String element = headerNames.nextElement();
            logger.debug("请求头 HEADER: {} --> {}", element, request.getHeader(element));
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                                Exception ex) throws Exception {
        logger.debug("--------------------------------------------");
    }
}
